home *** CD-ROM | disk | FTP | other *** search
- unit ReadXLSheet3U;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- Grids, OleServer, Excel97;
-
- type
- TForm1 = class(TForm)
- SG: TStringGrid;
- XL: TExcelApplication;
- XLSheet: TExcelWorksheet;
- procedure FormCreate(Sender: TObject);
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- procedure TForm1.FormCreate(Sender: TObject);
- var
- X, Y: Integer;
- Range: Variant;
- const
- DefLocale = 0; //Default locale value
- begin
- XL.Workbooks.Open('c:\Versions.xls', EmptyParam, EmptyParam,
- EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
- EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
- DefLocale);
- XLSheet.ConnectTo(XL.ActiveSheet as _WorkSheet);
- //Select last cell
- XLSheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;
- //Set string grid size
- SG.ColCount := XL.ActiveCell.Column;
- SG.RowCount := XL.ActiveCell.Row;
- //Read all cells from top left to last cell as a Variant array
- Range := XL.Range['A1', XL.Cells.Item[SG.RowCount, SG.ColCount]].Value;
- XL.Quit;
- XLSheet.Disconnect;
- XL.Disconnect;
- //String grid cells are numbered from 0
- //XL cells are numbered from 1
- //Also, XL cells are indexed as (row, column), not (column, row)
- for X := 0 to SG.ColCount - 1 do
- for Y := 0 to SG.RowCount - 1 do
- SG.Cells[X, Y] := Range[Y + 1, X + 1];
- end;
-
- end.
-